//
//  YJBForgetPWViewController.m
//  YunJiaBaoFirst
//
//  Created by 腾云小白龍 on 16/7/12.
//  Copyright © 2016年 腾云小白龍. All rights reserved.
//

#import "YJBForgetPWViewController.h"
#import "YJBResetPasswordViewController.h"

@interface YJBForgetPWViewController ()
@property (strong, nonatomic) UIScrollView * mainScrView;
@property (strong, nonatomic) UIButton * reSetBtn;
@property (strong, nonatomic) UITextField *phoneNumTextField;
@property (strong, nonatomic) UITextField *verfyCodeTextField;
@property (strong, nonatomic) UIButton *verfyCodeButton;
@property (strong, nonatomic) NSTimer * timer;
@property (assign, nonatomic) int time;

@end

@implementation YJBForgetPWViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
    [self setNavigationItemWithTitle:@"手机找回密码"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -- 初始化定时器
-(NSTimer *)timer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeVerfyBtnAction) userInfo:nil repeats:YES];
    }
    return _timer;
}

#pragma mark -- 60s 后再次获取
-(void)changeVerfyBtnAction{
    self.time --;
    NSString * content = @"已发送 ";
    NSString * total = [content stringByAppendingString:[[NSNumber numberWithInt:self.time] stringValue]];
    [self.verfyCodeButton setTitle:[total stringByAppendingString:@"s"] forState:UIControlStateNormal];
    
    if(self.time == 0) {
        [self.timer invalidate];
        _timer = nil;
        self.verfyCodeButton.enabled = YES;
        [self.verfyCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        
    }
    
    
}

#pragma mark -- 获取验证码
-(void)getVergyCode:(UIButton *)btn{
    
    if (!self.phoneNumTextField.text.length) {
        [SVProgressHUD showImage:nil status:@"请输入手机号码"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    BOOL isPhoneNum = [HTUtils isMobileNumber:self.phoneNumTextField.text];
    
    if (!isPhoneNum) {
        [SVProgressHUD showErrorWithStatus:@"您输入的手机号码不合法"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    
    btn.enabled = NO;
    self.time = 60;
    [self.timer fire];
}



-(void)initSubViews{
    self.mainScrView = [[UIScrollView alloc]initWithFrame:KMainScreen.bounds];
    self.mainScrView.contentSize = CGSizeMake(KMainScreenSize.width, KMainScreenSize.height + 1);
    self.mainScrView.scrollEnabled = YES;
    self.mainScrView.showsVerticalScrollIndicator   = NO;
    self.mainScrView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.mainScrView];
    
    CGFloat scaleH = KMainScreenSize.height / 667;
    CGFloat fontSize = 16;
    if (KMainScreenSize.height == 480) {
        fontSize = 13;
    }
    UILabel *cueLabel = ({
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, KMainScreenSize.width,scaleH* 44)];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"请输入您注册或者绑定的手机号码";
        label.font = [UIFont systemFontOfSize:fontSize];
        label.textColor = [UIColor blackColor];
        label;
    });
    [self.mainScrView addSubview:cueLabel];
    
    UIView *contentView = ({
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(cueLabel.frame),KMainScreenSize.width,scaleH* 88)];
        view.backgroundColor = [UIColor whiteColor];
        view;
    });
    [self.mainScrView addSubview:contentView];
    
    self.phoneNumTextField = ({
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(15,0,KMainScreenSize.width - 15 - 90,scaleH* 44)];
        textField.placeholder = @"中国 (+ 86)";
        textField.textAlignment = NSTextAlignmentLeft;
        textField.keyboardType = UIKeyboardTypePhonePad;
        textField.font = [UIFont systemFontOfSize:fontSize];
        [contentView addSubview:textField];
        textField;
    });

    UIView *lineViewNum = ({
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.phoneNumTextField.frame), contentView.frame.size.width - 20, 0.5)];
        view.backgroundColor = [UIColor grayColor];
        view;
    });
    [contentView addSubview:lineViewNum];
    
    self.verfyCodeButton = ({
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(contentView.frame.size.width - 15 -scaleH* 90, 7,scaleH* 90,scaleH* 30)];
        [button setTitle:@"获取验证码" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        button.backgroundColor = KMainColor;
        button.titleLabel.font = [UIFont systemFontOfSize:fontSize - 2];
        button.layer.cornerRadius = 5;
        button;
    });
    [contentView addSubview:self.verfyCodeButton];
    [self.verfyCodeButton addTarget:self action:@selector(getVergyCode:) forControlEvents:UIControlEventTouchUpInside];
    
    self.verfyCodeTextField = ({
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(lineViewNum.frame), KMainScreenSize.width - 30,scaleH* 44)];
        textField.placeholder = @"请输入验证码";
        textField.textAlignment = NSTextAlignmentLeft;
        textField.font = [UIFont systemFontOfSize:fontSize];
        [contentView addSubview:textField];
        textField.keyboardType = UIKeyboardTypePhonePad;
        textField;
    });

    
    self.reSetBtn = ({
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(contentView.frame) + 40, KMainScreenSize.width - 30,scaleH* 40)];
        [button setTitle:@"下一步" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        button.titleLabel.font = [UIFont systemFontOfSize:fontSize];
        button.backgroundColor = KMainColor;
        button;
    });
    [self.reSetBtn addTarget:self action:@selector(reSetBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.mainScrView addSubview:self.reSetBtn];
    
    
    
}

//下一步
-(void)reSetBtnClick{
    
    YJBResetPasswordViewController *vc = [[YJBResetPasswordViewController alloc]init];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController pushViewController:vc animated:YES];
    
    if (!self.phoneNumTextField.text.length) {
        [SVProgressHUD showImage:nil status:@"请输入手机号码"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    BOOL isPhoneNum = [HTUtils isMobileNumber:self.phoneNumTextField.text];
    
    if (!isPhoneNum) {
        [SVProgressHUD showErrorWithStatus:@"您输入的手机号码不合法"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    
    if (!self.verfyCodeTextField.text.length) {
        [SVProgressHUD showImage:nil status:@"请输入验证码"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    
}

-(void)SVProgressHUDdismiss{
    
    [SVProgressHUD dismiss];
    
}

@end
